#include <audio_encoder.h>
#define OUTPUT_CHANNELS 2

audio_encoder::audio_encoder(/* args */const char* codec_name,AVCodecParameters* codecpar)
{
    int error ;
     m_open_success = false;
      /* Find the encoder to be used by its name. */
    if (!(m_enc = avcodec_find_encoder(AV_CODEC_ID_AAC))) {
        fprintf(stderr, "Could not find an AAC encoder.\n");       
        return;
    }

    m_enc_ctx = avcodec_alloc_context3(m_enc);
    if (!m_enc_ctx) {
        fprintf(stderr, "Could not allocate an encoding context\n");       
        return ;
    }

    /* Set the basic encoder parameters.
     * The input file's sample rate is used to avoid a sample rate conversion. */
    m_enc_ctx->channels       = OUTPUT_CHANNELS;
    m_enc_ctx->channel_layout = av_get_default_channel_layout(OUTPUT_CHANNELS);
    error = avcodec_parameters_to_context(m_enc_ctx, codecpar);
     if (error < 0) {
        fprintf(stderr, "Could not initialize stream parameters\n");
        return;
    }
    /* Open the encoder for the audio stream to use it later. */
    if ((error = avcodec_open2(m_enc_ctx, m_enc, NULL)) < 0) {
        char errbuf[128];
        av_strerror(error, errbuf, sizeof(errbuf));

        fprintf(stderr, "Could not open output codec (error '%s' )\n",errbuf);                
        return;
    }   

    m_open_success = true;

}

audio_encoder::~audio_encoder()
{
}

int  audio_encoder::decode(AVPacket* pkt,AVFrame* frame,int eof,int& got,int* finished) 
{
    return 0;
}
int  audio_encoder::encode(const AVFrame*  frame, AVPacket* packet,int& got,int* finished) 
{
    int error;
    got = 0;
    /* Send the audio frame stored in the temporary packet to the encoder.
     * The output audio stream encoder is used to do this. */
    error = avcodec_send_frame(m_enc_ctx, frame);
    /* The encoder signals that it has nothing more to encode. */
    if (error == AVERROR_EOF) {
        error = 0;
        return -1;
    } else if (error < 0) {
         char errbuf[128];
        av_strerror(error, errbuf, sizeof(errbuf));
        fprintf(stderr, "Could not send packet for encoding (error '%s')\n",errbuf);
        return -1;;
    }

    /* Receive one encoded frame from the encoder. */
    error = avcodec_receive_packet(m_enc_ctx, packet);
    /* If the encoder asks for more data to be able to provide an
     * encoded frame, return indicating that no data is present. */
    if (error == AVERROR(EAGAIN)) {
        error = 0;
        return 0;
    /* If the last frame has been encoded, stop encoding. */
    } else if (error == AVERROR_EOF) {
        error = 0;
        return 0;
    } else if (error < 0) {
         char errbuf[128];
        av_strerror(error, errbuf, sizeof(errbuf));
        fprintf(stderr, "Could not encode frame (error '%s')\n",
                errbuf);
        return -1;
    /* Default case: Return encoded data. */
    } else {
        //*data_present = 1;
        packet->dts = frame->pkt_dts;
        packet->pts = frame->pkt_pts;
        got = 1;
    }
    return 0;
}
AVMediaType audio_encoder::get_codec_type() const{
    return AVMEDIA_TYPE_AUDIO;
}

